using System.Reflection;
using FluentValidation;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Zhaoxi.Zhihu.HotService;
using Zhaoxi.Zhihu.UseCases.AppUsers.Queries;
using Zhaoxi.Zhihu.UseCases.Common.Behaviors;
using Zhaoxi.Zhihu.UseCases.Questions.Job;

namespace Zhaoxi.Zhihu.UseCases;

public static class DependencyInjection
{
    public static IServiceCollection AddUserCaseServices(this IServiceCollection service)
    {
        service.AddAutoMapper(cfg => { }, Assembly.GetExecutingAssembly());

        service.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());


        service.AddMediatR(cfg =>
        {
            cfg.RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly());


            //全局注册
            cfg.RegisterServicesFromAssembly(Assembly.GetAssembly(typeof(Core.Dependencyinjection))!);

            cfg. RegisterServicesFromAssembly(Assembly.GetAssembly(typeof (HotService.DependencyInjection))!);
            //添加鉴权
            cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(AuthorizationBehavior<,>));

            // 添加验证行为 
            cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
        });

        service.AddSingleton<QuestionViewCountService>();
        service.AddHotService();

        return service;
    }
}